﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework03
{
    public partial class Form1 : Form
    {
        char flag = '0';
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'w' || e.KeyChar == 'a' || e.KeyChar == 's' || e.KeyChar == 'd') 
            {
                flag = e.KeyChar;   
                timer1.Enabled = true;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (flag == 'a')
            {
                this.pictureBox1.Image = Homework03.Properties.Resources.tk_left;
                this.pictureBox1.Left -= 4;
                if (this.pictureBox1.Left < -this.pictureBox1.Width)
                {
                    this.pictureBox1.Left = this.Width;
                }
            }
            else if (flag == 'd') 
            {
                this.pictureBox1.Image = Homework03.Properties.Resources.tk_right;
                this.pictureBox1.Left += 4;
                if (this.pictureBox1.Left > this.Width)
                {
                    this.pictureBox1.Left = -this.pictureBox1.Width;
                }
            } 
            else if (flag == 'w') 
            {
                this.pictureBox1.Image = Homework03.Properties.Resources.tk_up;
                this.pictureBox1.Top -= 4;
                if (this.pictureBox1.Top < -this.pictureBox1.Height)
                {
                    this.pictureBox1.Top = this.Height;
                }
            }
            else if(flag == 's') 
            {
                this.pictureBox1.Image = Homework03.Properties.Resources.tk_down;
                this.pictureBox1.Top += 4;
                if (this.pictureBox1.Top > this.Height)
                {
                    this.pictureBox1.Top = -this.pictureBox1.Height;
                }
            }
        }
    }
}
